<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户昵称" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="请输入用户昵称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="动态标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入动态标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" class="flex1" placeholder="请选择审核状态" style="width: 100%;">
          <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dynamic:dynamic:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['dynamic:dynamic:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['dynamic:dynamic:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dynamicList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="审核状态" align="center" prop="status">
        <template slot-scope="scope">
          <div v-if="scope.row.status == 0">审核通过</div>
          <div v-if="scope.row.status == 1">审核中</div>
          <div v-if="scope.row.status == 2">审核拒绝</div>
          <div v-if="scope.row.status == 3"><span style="color: red;">已删除</span></div>
        </template>
      </el-table-column>
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="动态标题" align="center" prop="title" />
      <!-- <el-table-column label="动态图片" align="center" prop="img">
        <template slot-scope="scope">
          <el-image style="width: 20px; height: 20px;margin-right: 5px;" :src="item"
            v-for="(item, index) in scope.row.img.split(',')" :key="index" @click="Screen(item)"></el-image>
        </template>
      </el-table-column> -->
      <el-table-column label="发布时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否置顶" align="center" prop="isTop" >
        <template slot-scope="scope">
          <div v-if="scope.row.isTop == 0">否</div>
          <div v-if="scope.row.isTop == 1">是</div>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="是否加入自动库" align="center" prop="isAutomatic" >
        <template slot-scope="scope">
          <div v-if="scope.row.isAutomatic == 0">否</div>
          <div v-if="scope.row.isAutomatic == 1">是</div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="是否自动刷新" align="center" prop="regular" >
        <template slot-scope="scope">
          <div v-if="scope.row.regular == 0">否</div>
          <div v-if="scope.row.regular == 1">24h</div>
          <div v-if="scope.row.regular == 2">48h</div>
        </template>
      </el-table-column> -->

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['dynamic:dynamic:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['dynamic:dynamic:remove']">删除</el-button>
            <el-button
               size="mini"
               type="text"
               icon="el-icon-circle-plus"
               @click="handleJoinAuto(scope.row)"
               v-hasPermi="['dynamic:dynamic:edit']"
             >加入库</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改用户发布动态/活动对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="动态标题" prop="title">
          <el-input v-model="form.title" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" class="flex1" placeholder="请选择" style="width: 100%;" @change="optionChange">
            <el-option v-for="item in typeImgOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

       <el-form-item label="内容" v-if="this.form.type == 1" prop="img">
         <div style="display: flex;align-items: center;flex-wrap: wrap;">
           <div style="position: relative;"  v-for="(item,index) in form.simpleSpectrum1" :key="index">
             <div style="position: absolute;
              top: 10px;
              right: 12px;
              color: rgb(240, 0, 0);
              background: #fff;
              width: 20px;
              line-height: 20px;
              text-align: center;
            height: 20px;" @click="handleRemove(index)">x</div>
             <img v-if="form.simpleSpectrum1.length > 0" :src="item" style="width: 100px;height: 100px;margin-right: 10px;margin-top: 10px;" class="avatar">
           </div>
           <el-upload class="avatar-uploader" action="" :http-request="ossUploadFunSpectrum" style="margin-top: 10px;" :show-file-list="false">
           	<i class="el-icon-plus avatar-uploader-icon" ></i>
           </el-upload>
         </div>
       </el-form-item>
      <el-form-item label="内容" v-if="this.form.type == 2" prop="img">
        <el-upload class="avatar-uploader" action="" :http-request="ossUploadFunSpectrum" :show-file-list="false">
        	<video v-if="form.img" :src="form.img" controls style="width: 250px;height: 100%;"></video>
        	<i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="是否置顶" prop="isTop">
        <el-select v-model="form.isTop" class="flex1" placeholder="请选择" style="width: 100%;">
          <el-option v-for="item in isTopOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="form.sort"  placeholder="请输入排序" />
      </el-form-item>
      <!-- <el-form-item label="是否自动刷新" prop="regular">
        <el-select v-model="form.regular" class="flex1" placeholder="请选择" style="width: 100%;">
          <el-option v-for="item in regularOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item> -->
     <!-- <el-form-item label="是否加入自动库" prop="isAutomatic">
        <el-select v-model="form.isAutomatic" class="flex1" placeholder="请选择" style="width: 100%;">
          <el-option v-for="item in automaticOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item> -->



       <el-form-item label="审核状态" prop="status">
         <el-select v-model="form.status" class="flex1" placeholder="请选择" style="width: 100%;">
           <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value">
           </el-option>
         </el-select>
       </el-form-item>
       <el-form-item label="拒绝理由" prop="rejected">
         <el-input v-model="form.rejected" type="textarea" placeholder="请输入内容" />
       </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
  </div>
</template>

<script>
import { listDynamic, getDynamic, delDynamic, addDynamic, updateDynamic,joinDynamicAuto } from "@/api/dynamic/dynamic";

export default {
  name: "Dynamic",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户发布动态/活动表格数据
      dynamicList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        title: null,
        nickName: null,
        isTop: 0,
        sort: 0,
        regular:0,
        img: null,
        type: null,
        dynamicType: 1,
        status: null,
        voice: null,
        lable: null,
        isVisible: null,
        begTime: null,
        endTime: null,
        serviceTime: null,
        likeCount: null,
        commentCount: null,
        giftCount: null,
        shareCount: null,
        enrollCount: null,
        rejected: null,
        adoptUser: null,
        amount: null,
        isAutomatic:0,
      },
      dialogImageUrl: '',
      dialogVisible: false,
      // 表单参数
      isTopOptions: [{
        value: 0,
        label: '否'
        }, {
          value: 1,
          label: '是'
      }],
      form: {
        simpleSpectrum1:[]
      },
      typeImgOptions: [{
        value: 1,
          label: '图片'
        }, {
          value: 2,
          label: '视频'
      }],
      statusOptions: [{
        value: 1,
        label: '审核中'
      }, {
        value: 0,
        label: '审核通过'
      }, {
        value: 2,
        label: '审核拒绝'
      }, {
        value: 3,
        label: '已删除'
      }],
      typeOptions: [{
        value: 1,
        label: '动态'
      }, {
        value: 2,
        label: '活动'
      }],
      isVisibleOptions: [{
        value: 1,
        label: '所有人'
      }, {
        value: 2,
        label: '会员'
      }],
      automaticOptions: [{
        value: 0,
        label: '否'
      }, {
        value: 1,
        label: '是'
      }],
      // regularOptions: [{
      //   value: 0,
      //   label: '否'
      // }, {
      //   value: 1,
      //   label: '24h'
      // }, {
      //   value: 2,
      //   label: '48h'
      // }],
      // 表单校验
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ]
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleRemove(index){
      this.form.simpleSpectrum1.splice(index,1);
    },
    Screen(e) {
      this.dialogImageUrl = e
      this.dialogVisible = true;
    },
    /** 查询用户发布动态/活动列表 */
    getList() {
      this.loading = true;
      listDynamic(this.queryParams).then(response => {
        this.dynamicList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleJoinAuto(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否将该动态加入自动库？').then(function() {
        return joinDynamicAuto(ids,row.userId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("加入成功");
      }).catch(() => {});
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        title: null,
        img: null,
        type: null,
        dynamicType: 1,
        status: 0,
        voice: null,
        lable: null,
        isVisible: null,
        begTime: null,
        endTime: null,
        serviceTime: null,
        remark: null,
        likeCount: null,
        commentCount: null,
        giftCount: null,
        shareCount: null,
        enrollCount: null,
        createTime: null,
        rejected: null,
        adoptUser: null,
        amount: null,
        isTop: null,
        isAutomatic:0,
        simpleSpectrum1:[]
      };
      this.resetForm("form");
    },
    optionChange(e) {
     this.form.simpleSpectrum1 = [];
     this.form.img = "";
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户发布动态/活动";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDynamic(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户发布动态/活动";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.img = this.form.simpleSpectrum1.toString();
          if (this.form.id != null) {
            updateDynamic(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDynamic(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 图片上传
    ossUploadFunVoice(file) {
    	this.ossUpload(file.file).then(res => {
    		this.form.voice = res
    	})
    },
    // 上传图片
    ossUploadFunSpectrum(file){
        if(file.file.type.indexOf('audio') != -1) {
         return this.$alert('请选择正确格式文件', '提示', {
           confirmButtonText: '确定',
         });
        }
        if(file.file.type.indexOf('image') != -1) {
          this.form.type = 1;
        }
        if(file.file.type.indexOf('video') != -1) {
          this.form.type = 2;
        }
        this.ossUpload(file.file).then(res => {
        this.form.simpleSpectrum1.push(res);
        this.form.img = res;
        })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户发布动态/活动编号为"' + ids + '"的数据项？').then(function () {
        return delDynamic(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dynamic/dynamic/export', {
        ...this.queryParams
      }, `dynamic_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
